package com.thinkgem.jeesite.modules.tb_driving_record.web;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.thinkgem.jeesite.common.persistence.Page;
import com.thinkgem.jeesite.common.utils.ToolsUtil;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.common.web.ErrorCodeAndMsg;
import com.thinkgem.jeesite.modules.tb_driving_record.entity.TbDrivingRecord;
import com.thinkgem.jeesite.modules.tb_driving_record.service.TbDrivingRecordService;
import com.thinkgem.jeesite.modules.tb_waybill.entity.TbWaybill;
import com.thinkgem.jeesite.modules.tb_waybill.service.TbWaybillService;

/**
 * 出车记录
 * @author zhangYangZe
 *
 */
@Controller
@RequestMapping("app/tbDrivingRecord/")
public class TbDrivingRecordAPPController   extends BaseController{
	
	@Autowired
	private TbDrivingRecordService tbDrivingRecordService;
	@Autowired
	private TbWaybillService waybillService;
	/**
	 * 添加
	 * @param req
	 * @param res
	 * @param waybill
	 * @param carId
	 * @param state
	 * @param mileage
	 * @param goodsAddOrRemove
	 * @param goods
	 * @param weight
	 * @param loss
	 * @param photoBig
	 * @param photoSmall
	 * @return
	 */
	@RequestMapping("save")
	@ResponseBody
	public Map<String,Object> save(
			HttpServletRequest req,
			HttpServletResponse res,
			@RequestParam("waybill")String waybill,
			@RequestParam("carId")String carId,
			@RequestParam("state")String state,
			@RequestParam(value="mileage",required=false)String mileage,
			@RequestParam(value="goodsAddOrRemove",required=false)String goodsAddOrRemove,
			@RequestParam(value="goods",required=false)String goods,
			@RequestParam(value="weight",required=false)String weight,
			@RequestParam(value="loss",required=false)String loss,
			@RequestParam(value="photoBig",required=false)String photoBig,
			@RequestParam(value="photoSmall",required=false)String photoSmall
			){
		int driverId = uid(req);
		if(driverId==-1)return returnError(ErrorCodeAndMsg.USER_NULL_CODE, ErrorCodeAndMsg.USER_NULL_CODE_MSG);
		try{
			
			TbDrivingRecord tbDrivingRecord = new TbDrivingRecord();
			tbDrivingRecord.setCreatDate(new Date());
			tbDrivingRecord.setWaybill(waybill);
			tbDrivingRecord.setCarId(carId);
			tbDrivingRecord.setState(state);
			if(ToolsUtil.isTrue(goods))tbDrivingRecord.setGoods(goods);
			if(ToolsUtil.isTrue(goodsAddOrRemove))tbDrivingRecord.setGoodsAddOrRemove(goodsAddOrRemove);
			if(ToolsUtil.isTrue(mileage))tbDrivingRecord.setMileage(Double.parseDouble(mileage));
			if(ToolsUtil.isTrue(weight))tbDrivingRecord.setWeight(Double.parseDouble(weight));
			if(ToolsUtil.isTrue(loss))tbDrivingRecord.setLoss(Double.parseDouble(loss));
			if(ToolsUtil.isTrue(photoBig))tbDrivingRecord.setPhotoBig(photoBig);
			if(ToolsUtil.isTrue(photoSmall))tbDrivingRecord.setPhotoSmall(photoSmall);
			
			
			tbDrivingRecordService.save(tbDrivingRecord);
		}catch(Exception e){
			e.printStackTrace();
			return returnError(">>>添加车辆运输记录异常", e);
		}
		return returnSuccess();
	}
	/**
	 * 修改
	 * @param req
	 * @param res
	 * @param id
	 * @param waybill
	 * @param carId
	 * @param state
	 * @param mileage
	 * @param goodsAddOrRemove
	 * @param goods
	 * @param weight
	 * @param loss
	 * @param photoBig
	 * @param photoSmall
	 * @return
	 */
	@RequestMapping("update")
	@ResponseBody
	public Map<String,Object> update(
			HttpServletRequest req,
			HttpServletResponse res,
			@RequestParam("id")String id,
			@RequestParam("waybill")String waybill,
			@RequestParam("carId")String carId,
			@RequestParam("state")String state,
			@RequestParam(value="mileage",required=false)String mileage,
			@RequestParam(value="goodsAddOrRemove",required=false)String goodsAddOrRemove,
			@RequestParam(value="goods",required=false)String goods,
			@RequestParam(value="weight",required=false)String weight,
			@RequestParam(value="loss",required=false)String loss,
			@RequestParam("photoBig")String photoBig,
			@RequestParam("photoSmall")String photoSmall
			
			){
		int driverId = uid(req);
		if(driverId==-1)return returnError(ErrorCodeAndMsg.USER_NULL_CODE, ErrorCodeAndMsg.USER_NULL_CODE_MSG);
		try{
			
			TbDrivingRecord tbDrivingRecord = new TbDrivingRecord();
			tbDrivingRecord.setId(id);
			tbDrivingRecord.setWaybill(waybill);
			tbDrivingRecord.setCarId(carId);
			tbDrivingRecord.setState(state);
			if(ToolsUtil.isTrue(goods))tbDrivingRecord.setGoods(goods);
			if(ToolsUtil.isTrue(goodsAddOrRemove))tbDrivingRecord.setGoodsAddOrRemove(goodsAddOrRemove);
			if(ToolsUtil.isTrue(mileage))tbDrivingRecord.setMileage(Double.parseDouble(mileage));
			if(ToolsUtil.isTrue(weight))tbDrivingRecord.setWeight(Double.parseDouble(weight));
			if(ToolsUtil.isTrue(loss))tbDrivingRecord.setLoss(Double.parseDouble(loss));
			tbDrivingRecord.setPhotoBig(photoBig);
			tbDrivingRecord.setPhotoSmall(photoSmall);
			tbDrivingRecordService.save(tbDrivingRecord);
			
		}catch(Exception e){
			e.printStackTrace();
			return returnError(">>>添加车辆运输记录异常", e);
		}
		return returnSuccess();
	}
	
	/**
	 * 列表
	 * @param req
	 * @param res
	 * @param waybill
	 * @param carId
	 * @param pagNo
	 * @param pageSize
	 * @return
	 */
	@RequestMapping("list")
	@ResponseBody
	public Map<String,Object> list(
			HttpServletRequest req,
			HttpServletResponse res,
			@RequestParam("waybill")String waybill,
			@RequestParam("carId")String carId,
			@RequestParam("pagNo")String pagNo,
			@RequestParam("pageSize")String pageSize
			){
		Map<String,Object> results = new HashMap<String, Object>();
		List<Map<String,Object>> resultsList = new ArrayList<Map<String,Object>>();
		int driverId = uid(req);
		if(driverId==-1)return returnError(ErrorCodeAndMsg.USER_NULL_CODE, ErrorCodeAndMsg.USER_NULL_CODE_MSG);
		try{
			TbDrivingRecord tbDrivingRecord = new TbDrivingRecord();
			tbDrivingRecord.setWaybill(waybill);
			tbDrivingRecord.setCarId(carId);
			Page<TbDrivingRecord> page = tbDrivingRecordService.findPage(new Page<TbDrivingRecord>(req, res), tbDrivingRecord); 
			int pagN = page(pagNo,0);
			int pageS = page(pageSize,50);
			page.setPageNo(pagN);
			page.setPageSize(pageS);
			List<TbDrivingRecord> list = page.getList();
			if(list.size()>0){
				for(int i=0;i<list.size();i++){
					TbDrivingRecord t = list.get(i);
					Map<String,Object> m = new HashMap<String, Object>();
					String state = t.getState();
					String context = "";
					if("0".equals(state)||"3".equals(state)){
						if("0".equals(state)){
							context = "车库出发  里程数【"+t.getMileage()+"】";
						}else{
							context = "车辆回库  里程数【"+t.getMileage()+"】";
						}
					}else{
						if("1".equals(state)){
							context =t.getTbLoadingAddress().getAddress()+"装货  "+t.getTbGoods().getGoodName()+"吨 亏"+t.getLoss();
						}else{
							context = t.getTbDischargeCargoAddress().getAddress()  +"卸货  "+t.getTbGoods().getGoodName()+"吨 亏"+t.getLoss();
						}
					}
					m.put("tbDrivingRecord_state",state  );
					m.put("tbDrivingRecord_context",  context );
					m.put("tbDrivingRecord_photo_big", ToolsUtil.strToStrArray(t.getPhotoBig())   );
					m.put("tbDrivingRecord_photo_small", ToolsUtil.strToStrArray(t.getPhotoSmall())   );
					m.put("tbDrivingRecord_date", ToolsUtil.dateFormate(t.getCreatDate(), "yyyy-MM-dd HH:mm"));
					resultsList.add(m);
				}
			}
			results.put("list", resultsList);
			TbWaybill tbWaybill = waybillService.get(waybill);
			Map<String,String> map = new HashMap<String, String>();
			map.put("tbWaybill_car",tbWaybill.getTbCarInfo().getCarCode()  );//车牌号
			map.put("tbWaybill_driver", tbWaybill.getTbDriver().getDriverName()+" "+tbWaybill.getTbDriverTwo().getDriverName() );//司机
			//map.put("tbWaybill_good", tbWaybill.getTbGoods().getGoodName() );//货物
			//map.put("tbWaybill_weight", tbWaybill.getWeight()+"吨" );
			map.put("tbWaybill_tbLoadingAddress", tbWaybill.getTbLoadingAddress().getAddress() );//装货地址
			//map.put("tbWaybill_tbDischargeCargoAddress", tbWaybill.getTbDischargeCargoAddress().getAddressNew() );//卸货地址
			
			
			
			
			
			results.put("header", map);
		}catch(Exception e){
			e.printStackTrace();
			return returnError(">>>车辆运输记录列表异常", e);
		}
		return returnSuccess(results);
		
	}
	
}
